import lrz from 'lrz'
import request from '../utils/httpRequest'
import store from '../store'
import rebounce from 'rebounce';
import { formatMoney } from './filter'
import { formatDate, isEmpty, regexp } from '../utils/tools'
import GoodsChoose from '../components/jump-choose/goods'
import GrassChoose from '../components/jump-choose/grass'
import UsersChoose from '../components/jump-choose/users'
import ActivityList from '../components/jump-choose/activityList'
import ActivityGoods from '../components/jump-choose/activityGoods'
import GoodsMiddleStageGoodsChoose from '../components/goodMiddleStage/goods' // 商品中台商品
import ComboPackage from '../components/jump-choose/comboPackage'
import PackageChoose from '../components/jump-choose/package'
import PartnerChoose from '../components/jump-choose/partner'
import BoxChoose from '../components/jump-choose/box'
import boxGoodChoose from '../components/goods-choose/boxGoodChoose'
import BrandChoose from '../components/goods-choose/box'
import AreaChoose from '../components/jump-choose/area'
import ActivityChoose from '../components/jump-choose/activity'
import SpecialChoose from '../components/jump-choose/special'
import CouponChoose from '../components/jump-choose/coupon'
import GoodsCheck from '../components/goods-choose/goods'
import PackageCheck from '../components/goods-choose/package'
import SpecialCheck from '../components/goods-choose/special'
import ActivityCheck from '../components/goods-choose/activity'
import boxCheck from '../components/goods-choose/boxSeries'
import boxPackage from '../components/goods-choose/boxPackage'
import materialCheck from '../components/jump-choose/materialLibrary'
import BhsGoods from '../components/bihuasuan/goods-choose/goods'
import BhsZeroGoods from '../components/bihuasuan/goods-choose/goodsZerobuy'
import BhsGoodsTag from '../components/bihuasuan/goods-choose/goodsTag'
import BhsGoodsPlatform from '../components/bihuasuan/goods-choose/goodsPlatform'
import BihsAdChoose from '../components/bihuasuan/ad-choose/index'
import BhstaobaoActivity from '../components/bihuasuan/taobao-activity/taobaoActivity'
import { debounce } from '../utils/index';
import { upload, uploadShot } from '../api/common';
import propertyGroup from '../components/goodMiddleStage/propertyGroup'
import skuSelect from '@/components/goods-choose/skuSelect.vue'
let timer = null;
var numberCheck = (rule, value, callback) => {
  console.log(value)
  if (value < 1 || value > 7) {
    callback(new Error('签到天数范围只能1~7'));
  } else {
    callback();
  }
};
export default {
  install(Vue) {
    Vue.filter("money", formatMoney);

    // 注入组件
    Vue.mixin({
      data() {
        let btns = this.$route && this.$route.meta && this.$route.meta.buttons || {}
        let rightsButtons = {}
        for(let key in btns){
          if(btns[key].enabled){
            rightsButtons[key] = btns[key]
          }
        }
        return {
          rightsButtons: rightsButtons
        }
      },
      updated: debounce(function () {
        // console.log('user operated');
        clearTimeout(timer);
        timer = setTimeout(() => {
          store.dispatch('FedLogOut').then(() => {
            location.reload() // 为了重新实例化vue-router对象 避免bug
          })
        }, 1000 * 60 * 60);
      }, 1000, false)
    });

    /**
     * 自定义方法/属性
     */
    Vue.prototype.request = request;
    Vue.prototype.base = process.env.BASE_API;
    Vue.prototype.upladUrl = `${process.env.UPLOAD_BASE_API}/bsnl-oss/upload`
    Vue.prototype.uploadShotUrl = `${process.env.UPLOAD_BASE_API}/bsnl-oss/uploadShot`
    Vue.prototype.excelUpload = `${process.env.MESSAGE_BASE_API}/mc-manage/mobile/upload`
    Vue.prototype.bonusUpload = `${process.env.PRODUCT_BASE_API}/bsnl-product/box/bonus/config/importBoxConfigDetail`
      // 商品中台 属性组管理 single:是否单选 check:当前选中项， maxCount:最多选中数
    Vue.prototype.goodsMiddleStageCat = (single, check, maxCount) => {
        return new Promise((res, rej) => {
          let cat = new Vue({
            methods: {
              choose(val) {
                res(val);
              }
            },
            render(h) {
              // console.log('闭环是', count)
              return (
                <propertyGroup onChoose={this.choose} single={single} check={check} maxCount={maxCount} />
              )
            },
            components: {
              propertyGroup
            }
          }).$mount();
          document.body.appendChild(cat.$el);
        })
      }
    // 列表索引
    Vue.prototype.indexMethod = function(index, listQuery) {
      return (
        ((listQuery.pageIndex || 1) - 1) * (listQuery.pageSize || 20) +
        index +
        1
      );
    };
    // 替换图片路径
    Vue.prototype.replaceImgUrl = (imgUrl) => {
      if (imgUrl && typeof imgUrl === 'string') {
        return imgUrl.replace('http://pcefuo8sm.bkt.clouddn.com', 'https://img.bisinuolan.cn')
      }
      return "";
    };
    // 文件上传之前判断，如是图片，则上传之前压缩
    Vue.prototype.beforeUpload = function (file, option) {
      let limit = (option && option.limitSize) ? option.limitSize : 1;         // 文件大小限制，默认：1M
      // let quality = (option && option.qualitySize) ? option.qualitySize : 0.7; // 图片文件压缩率，默认：0.7

      if (file.size > limit * 1024 * 1024) {
        this.$message.error(`文件不能大于${limit}M`);
        return false;
      }
      if (/image/.test(file.type)) {
        if (file.size > 300 * 1024) {
          this.$message.error(`图片超过300KB,会影响APP展示效果,建议压缩后上传`);
        }
      }
      /* if (/image\/jpeg/.test(file.type) || /image\/jpg/.test(file.type)) {
        return new Promise((resolve, reject) => {
          lrz(file, { quality })
            .then((res) => {
              resolve(res.file)
            })
            .catch(e => {
              reject(e)
            })
        })
      } */
    };
    // 图片压缩上传
    Vue.prototype.picFileUpload = function (file, callback, option, isAdvice, shot) {
      let limit = (option && option.limitSize) ? option.limitSize : 1;         // 文件大小限制，默认：1M
      let quality = (option && option.qualitySize) ? option.qualitySize : 0.7; // 图片文件压缩率，默认：0.7
      let type = 2
      if (!/image/.test(file.type)) {
        type = 1;
        // this.$message.error('只能上传图片');
        // return false;
      } else {
        if (file.size > limit * 1024 * 1024) {
          this.$message.error(`文件不能大于${limit}M`);
          return false;
        }
        if (!isAdvice) {
          if (file.size > 300 * 1024) {
            this.$message.error(
              `图片超过300KB,会影响APP展示效果,建议压缩后上传`
            );
          }
        }
      }
      let formData = new FormData();
      formData.append("file", file);
      if(shot==='shot'){
        formData.append("shot", true)
        uploadShot(formData)
        .then((data) => {
          callback(data, file, type)
        })
        .catch(e => {
          console.log(e);
        });
      }else{
        upload(formData)
        .then((data) => {
          callback(data, file, type)
        })
        .catch(e => {
          console.log(e);
        });
      }
      /*lrz(file, { quality })
        .then((res) => {
          upload(res.formData)
            .then((data) => {
              callback(data, file)
            })
            .catch(e => {
              console.log(e)
            })
        })
        .catch(e => {
          console.log(e)
        })*/
    };
    // 多次快速点击只响应一次
    Vue.prototype.rebounce = rebounce;
    // 是否为空
    Vue.prototype.isEmpty = isEmpty;
    // 格式化日期
    Vue.prototype.formatDate = formatDate;
    // 公共验证规则
    Vue.prototype.commonRules = {
      daysCheck: [
        { required: true, message: '不能为空', trigger: 'change' },
        { validator: numberCheck, trigger: 'blur' }
      ],
      notNullRule: [
        { required: true, message: '必填项不能为空', trigger: 'change' },
      ],
      notNullSelectRule: [
        { required: true, message: '请选择', trigger: 'change' },
      ],
      fiftyThousandRule:[
        { pattern: regexp.fiftyThousand, message: '须小于等于50000的正整数', trigger: 'change' }
      ],
      priceRule: [
        { required: true, message: '不能为空', trigger: 'change' },
        { pattern: regexp.money, message: '须大于0小于1000000且最多保留两位小数', trigger: 'change' }
      ],
      notZeroRule: [
        { required: true, message: '不能为空', trigger: 'change' },
        { pattern: regexp.money, message: '不能为空', trigger: 'change' }
      ],
      priceRuleNull: [//非必填价格规则
        { pattern: regexp.money, message: '须大于0小于1000000且最多保留两位小数', trigger: 'change' }
      ],
      priceRuleZero: [//可为0价格规则
        { required: true, message: '不能为空', trigger: 'change' },
        { pattern: regexp.zMoney, message: '须大于等于0,小于100000000且最多保留两位小数', trigger: 'change' }
      ],
      priceRuleZeroNull: [//可为0非必填规则
        { pattern: regexp.zMoney, message: '须大于等于0,小于100000000且最多保留两位小数', trigger: 'change' }
      ],
      priceRuleZeroT: [//可为0价格规则
        { required: true, message: '不能为空', trigger: 'change' },
        { pattern: regexp.zMoney, message: '大于等于0,最多保留两位小数', trigger: 'change' }
      ],
      groupRule: [
        { required: true, message: '不能为空', trigger: 'change' },
        { pattern: regexp.group, message: '必须是大于2且小于等于100的数字', trigger: 'change' }
      ],
      percentRule: [
        { required: true, message: '不能为空', trigger: 'change' },
        { pattern: regexp.percent, message: '必须大于0且小于等于100且最多两位小数', trigger: 'change' }
      ],
      skuNumberRule: [
        { required: true, message: '不能为空', trigger: 'change' },
        { pattern: regexp.int, message: '必须为非负整数', trigger: 'change' }
      ],
      numberRule: [
        { required: true, message: '不能为空', trigger: 'change' },
        { pattern: regexp.positiveInt, message: '必须为正整数', trigger: 'change' }
      ],
      fakerNumRule: [
        { pattern: regexp.positiveInt, message: '必须为正整数', trigger: 'change' }
      ],
      numberRuleZero: [
        { required: true, message: '不能为空', trigger: 'change' },
        { pattern: regexp.int, message: '必须为非负整数', trigger: 'change' }
      ],
      floatRule: [
        { required: true, message: '不能为空', trigger: 'change' },
        { pattern: regexp.float, message: '请输入正确的数字', trigger: 'change' }
      ],
      sortRule: [
        /*{required: true, message: '不能为空', trigger: 'change'},*/
        { pattern: regexp.sort, message: '必须为0到9999的整数值', trigger: 'change' }
      ],
      urlRule: [
        { required: true, message: '不能为空', trigger: 'change' },
        { pattern: regexp.url, message: '必须为正确的URL格式', trigger: 'change' }
      ],
      nullUrlRule: [
        { pattern: regexp.url, message: '必须为正确的URL格式', trigger: 'change' }
      ],
      ignoreInvite: [
        { pattern: regexp.ignoreInvite, message: 'URL中不可包含邀请码', trigger: 'change' }
      ]
    };
    //商品中台 商品库
    Vue.prototype.gmsGoodsChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            // console.log('闭环是', count)
            return (
              <GoodsMiddleStageGoodsChoose  config={config} onChoose={this.choose} selected={selected}/>
            )
          },
          components: {
            GoodsMiddleStageGoodsChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    }
    //选择商品——碧划算
    Vue.prototype.bhsGoodsChoose = (count=1, check=[], selectedMore) => {

      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            // console.log('闭环是', count)
            return (
              <bhs-Goods count={count} check={check} onChoose={this.choose} selectedMore={selectedMore} />
            )
          },
          components: {
            BhsGoods
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    }
    // 淘宝官方活动筛选
    Vue.prototype.bhstaobaoActivity = (selected, count) => {
      count = Number(count);
      if (isNaN(count)) {
        count = 0;
      }
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <BhstaobaoActivity check={selected} count={count} onChoose={this.choose} />
            )
          },
          components: {
            BhstaobaoActivity
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    //碧划算商品栏目
    Vue.prototype.bhsGoodsTag = (selected, count, skuFlag) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <BhsGoodsTag count={count} selected={selected} skuFlag={skuFlag} onChoose={this.choose} />
            )
          },
          components: {
            BhsGoodsTag
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    }
    //选择商品——碧划算0元购
    Vue.prototype.bhsZeroGoodsChoose = (count=1, check=[], selectedMore) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            // console.log('闭环是', count)
            return (
              <bhs-zero-Goods count={count} check={check} onChoose={this.choose} selectedMore={selectedMore} />
            )
          },
          components: {
            BhsZeroGoods
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    }
    // 碧划算平台
    Vue.prototype.bhsGoodsPlatForm = (selected, count, skuFlag) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <BhsGoodsPlatform count={count} selected={selected} skuFlag={skuFlag} onChoose={this.choose} />
            )
          },
          components: {
            BhsGoodsPlatform
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    }
    //碧划算广告选择
    Vue.prototype.bihsAdChoose = (selected, count, position, disable, enabled) => {
      count = Number(count);
      if (isNaN(count)) {
        count = 0;
      }
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <BihsAdChoose check={selected} count={count} onChoose={this.choose} position={position} disable={disable} enabled={enabled} />
            )
          },
          components: {
            BihsAdChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择用户[新]
    Vue.prototype.usersChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <users-choose config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            UsersChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择商品[新]
    Vue.prototype.goodsChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <goods-choose config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            GoodsChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择种草素材
    Vue.prototype.grassChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let grassChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <grass-choose config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            GrassChoose
          }
        }).$mount();
        document.body.appendChild(grassChoose.$el);
      });
    };
    // 选择组合套餐
    Vue.prototype.comboPackageChoose = (selected, count) => {
      count = Number(count);
      if (isNaN(count)) {
        count = 0;
      }
      return new Promise((resolve, reject) => {
        let comboChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <combo-package check={selected} count={count} onChoose={this.choose} />
          )
          },
          components: {
            ComboPackage
          }
        }).$mount();
        document.body.appendChild(comboChoose.$el);
      });
    };
    // h5活动列表
    Vue.prototype.activityList = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <activity-list config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            ActivityList
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    //活动列表自定义H5选择的商品
    Vue.prototype.activityGoods = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <activity-goods config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            ActivityGoods
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择套餐[新]
    Vue.prototype.packageChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <package-choose config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            PackageChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择城市合伙人[新]
    Vue.prototype.partnerChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <partner-choose config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            PartnerChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择箱起零售活动/箱起云仓商品
    Vue.prototype.brandChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <brand-choose config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            BrandChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择独立箱[新]
    Vue.prototype.boxChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <box-choose config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            BoxChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    //选择箱起商品
    Vue.prototype.boxGoodChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <boxGoodChoose config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            boxGoodChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择地区 [新]
    Vue.prototype.areaChoose = (config, selected, disChose) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <area-choose config={config} selected={selected} unChose={disChose} onChoose={this.choose} />
            )
          },
          components: {
            AreaChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择活动[新]
    Vue.prototype.activityChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <activity-choose config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            ActivityChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择专题[新]
    Vue.prototype.specialChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <special-choose config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            SpecialChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择优惠券[新]
    Vue.prototype.couponChoose = (config, selected) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <coupon-choose config={config} selected={selected} onChoose={this.choose} />
            )
          },
          components: {
            CouponChoose
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };

    // 选择商品
    Vue.prototype.checkGoods = (selected, count, skuFlag, goodsType) => {
      count = Number(count);
      if (isNaN(count)) {
        count = 0;
      }
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <goods-check check={selected} count={count} skuFlag={skuFlag} goodsType={goodsType} onChoose={this.choose} />
            )
          },
          components: {
            GoodsCheck
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };

    // sku选择
    Vue.prototype.$skuSelect = ({id, multiple}) => {
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <skuSelect id={id} multiple={multiple} onChoose={this.choose} />
            )
          },
          components: {
            skuSelect
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };

    // 选择箱起
    Vue.prototype.boxSeries = (selected, count, seriesType, hasSelected) => {
      count = Number(count);
      if (isNaN(count)) {
        count = 0;
      }
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <box-check check={selected} count={count} seriesType={seriesType} hasSelected={hasSelected} onChoose={this.choose} />
            )
          },
          components: {
            boxCheck
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };

    // 选择箱起礼包
    Vue.prototype.boxPackageGet = (selected, count) => {
      count = Number(count);
      if (isNaN(count)) {
        count = 0;
      }
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <box-package check={selected} count={count} onChoose={this.choose} />
            )
          },
          components: {
            boxPackage
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择素材库里面的素材
    Vue.prototype.materialLibrary = (selected, count, materialType) => {
      count = Number(count);
      if (isNaN(count)) {
        count = 0;
      }
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val)
            }
          },
          render(h) {
            return (
              <material-check check={selected} count={count} materialType={materialType} onChoose={this.choose} />
            )
          },
          components: {
            materialCheck
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择套餐
    Vue.prototype.checkPackage = (selected, count) => {
      count = Number(count);
      if (isNaN(count)) {
        count = 0;
      }
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <package-check check={selected} count={count} onChoose={this.choose} />
            )
          },
          components: {
            PackageCheck
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择专题
    Vue.prototype.checkSpecial = (selected, count) => {
      count = Number(count);
      if (isNaN(count)) {
        count = 0;
      }
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <special-check
                check={selected}
                count={count}
                onChoose={this.choose}
              ></special-check>
            );
          },
          components: {
            SpecialCheck
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };
    // 选择活动
    Vue.prototype.checkActivity = (selected, count) => {
      count = Number(count);
      if (isNaN(count)) {
        count = 0;
      }
      return new Promise((resolve, reject) => {
        let userChoose = new Vue({
          methods: {
            choose(val) {
              resolve(val);
            }
          },
          render(h) {
            return (
              <activity-check
                check={selected}
                count={count}
                onChoose={this.choose}
              ></activity-check>
            );
          },
          components: {
            ActivityCheck
          }
        }).$mount();
        document.body.appendChild(userChoose.$el);
      });
    };

    // 计算大区价和兰蜜价
    Vue.prototype.calcRDPrice = (queryData, setData) => {
      // let query = {
      //   lmPrice: parseFloat(queryData.lmPrice),
      //   highestCommission: parseFloat(queryData.highestCommission)
      // };
      let query = {
        price: parseFloat(queryData.lmPrice)
      };
      /*console.log(query.lmPrice!=='')
      console.log(query.lmPrice>0)
      console.log(query.highestCommission!=='')
      console.log(query.highestCommission>=0)
      console.log(query.lmPrice>query.highestCommission)*/
      if (query.price !== '' && query.price > 0) {
        request({
          url: "/bsnl-product/goods/v2/calculatePrice",
          method: "get",
          params: query
        }).then(({ data }) => {
          setData(data);
        }).catch(err => {
          setData({
            regionPrice: '',
            directorPrice: '',
            diamondPrice: ''
          });
        });
      }
      else {
        setData({
          regionPrice: "",
          directorPrice: ""
        });
      }
    };

    const H5Url = {
      test: "http://172.16.6.122:9013",
      uat: "https://h5-uni-uat.bisinuolan.cn",
      prod: "https://h5-uni.bisinuolan.cn"
    }
    // 预览商品详情地址
    Vue.prototype.getUrl = config => {
      return `${H5Url[process.env.ENV_CONFIG]}/pagesDetail/commodity/index?id=${config.id}&from_type=${config.fromType}&goods_type=${config.goodsType}&activity_id=${config.activityId}&pack_type=${config.packType || 0}`;
    };
    // 预览活动详情地址
    Vue.prototype.getActivityUrl = config => {
      let params = ''
      let char = ''
      for (let key in config) {
        char = (char === '') ? '?' : '&'
        params += `${char}${key}=${config[key]}`
      }

      return `${H5Url[process.env.ENV_CONFIG]}/pagesDetail/template/index${params}`;
    };
    Vue.prototype.getActivityUrlTwo = config => {
      let url = {
        test: "http://172.16.6.122:9009/H5/#",
        uat: "https://m-uat.bisinuolan.cn/H5/#",
        prod: "https://m.bisinuolan.cn/H5/#"
      };

      let params = ''
      let char = ''
      for (let key in config) {
        char = (char === '') ? '?' : '&&'
        params += `${char}${key}=${config[key]}`
      }

      return `${url[process.env.ENV_CONFIG]}/detail/promotion${params}`;
    };
    //导出文件
    Vue.prototype.downloadFile = function(data, filename) {
      if (!data) {
        return false;
      } else {
        if ("msSaveOrOpenBlob" in navigator) {
          window.navigator.msSaveOrOpenBlob(
            new Blob([data]),
            filename || "excel.xls"
          );
        } else {
          let url = window.URL.createObjectURL(new Blob([data]));
          let link = document.createElement("a");
          link.style.display = "none";
          link.href = url;
          link.setAttribute("download", filename || "excel.xls");
          document.body.appendChild(link);
          link.click();
        }
      }
    };
    //获取页面listQuery
    Vue.prototype.getLocalQuery = function(initQuery, pageName) {
      let query = sessionStorage.getItem(`page_${pageName}_list_query`);
      if (query) {
        return Object.assign({}, initQuery, JSON.parse(query));
      }
      return initQuery;
    };
    //缓存页面listQuery
    Vue.prototype.setLocalQuery = function(listQuery, pageName) {
      sessionStorage.setItem(
        `page_${pageName}_list_query`,
        JSON.stringify(listQuery)
      );
    };
  }
};
